<template>
  <div class="main-box">
    <Table :request-data="requestData" v-on:list="listHander" ref="table">
      <template slot="search-box">
        <!-- 关键词 -->
        <el-form-item label="关键词">
          <el-input style="width:100px" size="small" v-model="requestData.postData.keyword"></el-input>
        </el-form-item>
        <!-- 商品价格-->
        <el-form-item label="商品价格" style="margin-left:10px;">
          <el-input style="width:50px" size="small" v-model="requestData.postData.start_price"></el-input>
          <span style="padding-left:5px;padding-right:5px;">-</span>
          <el-input style="width:50px" size="small" v-model="requestData.postData.end_price"></el-input>
        </el-form-item>
        <!-- 游戏名称 -->
        <el-form-item label="游戏区服" style="margin-left:10px;">
          <el-select
            size="small"
            placeholder="请选择"
            style="width:100px"
            v-model="requestData.postData.belongs"
          >
            <el-option label="全部游戏" value="-1"></el-option>
            <el-option v-for="item of games" :label="item.name" :value="item.code"></el-option>
          </el-select>
        </el-form-item>
        <!-- 商品类型 -->
        <el-form-item label="物品类型" style="margin-left:10px;">
          <el-select
            size="small"
            placeholder="请选择"
            style="width:100px"
            v-model="requestData.postData.goods_type"
          >
            <el-option label="全部类型" value="-1"></el-option>
            <el-option v-for="item of goodsType" :label="item.key" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-left:10px;">
          <el-button size="small" @click="search" round>查询</el-button>
        </el-form-item>
      </template>
      <template slot="table-box">
        <el-table :data="tableData" stripe style="width: 100%" height="520">
          <el-table-column label="添加日期" width="170">
            <template slot-scope="scope">
              <span>{{ $utils.formatDateTime(scope.row.add_time)}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="title" label="标题">
            <template slot-scope="scope">
              <a class="link">
                <span
                  class="one-line"
                  :style="scope.row.end_time==0?'color:#f56c6c':''"
                >{{scope.row.title}}</span>
              </a>
            </template>
          </el-table-column>
          <el-table-column prop="price" label="价格(积分)" width="120">
            <template slot-scope="scope">
              <span>{{scope.row.price}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="quantity" label="库存" width="80"></el-table-column>
          <el-table-column prop="belongs" label="游戏区服" width="120">
            <template slot-scope="scope">
              <span>{{ getBelongs(scope.row.belongs)}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="goods_type" label="物品类型" width="120">
            <template slot-scope="scope">
              <span>{{ getGoodsType(scope.row.goods_type)}}</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="80">
            <template slot-scope="scope">
              <el-button
                size="mini"
                round
                type="warning"
                @click="handleShowDialog(scope.$index)"
              >修 改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </Table>
    <el-dialog :visible.sync="dialogVisible" custom-class="dialog" @close="handleDialogClose">
      <el-row style="padding:30px 0;">
        <el-col :span="24" class="main-h-center">
          <el-form label-position="right" label-width="50px" style="width:700px;">
            <!--  -->
            <el-form-item label="标题">
              <el-input v-model="currentModify.title" placeholder="商品主标题"></el-input>
            </el-form-item>
            <!--  -->
            <el-form-item label="详情">
              <el-input
                v-model="currentModify.subtitle"
                placeholder="商品详情内容"
                type="textarea"
                :rows="3"
              ></el-input>
            </el-form-item>
            <!--  -->
            <el-form-item label="价格">
              <el-col :span="6">
                <el-form-item label>
                  <el-input v-model="currentModify.price" placeholder="商品价格" type="number"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="库存" style="margin-left:20px;">
                  <el-input v-model="currentModify.quantity" placeholder="库存数量" type="number"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="过期" style="margin-left:20px;">
                  <el-input v-model="currentModify.end_time" placeholder="单位（天）" type="number"></el-input>
                </el-form-item>
              </el-col>
            </el-form-item>
            <!--  -->
            <el-form-item label="游戏">
              <el-col :span="6">
                <el-form-item>
                  <el-select placeholder="选择游戏" v-model="currentModify.belongs">
                    <el-option v-for="item of games" :label="item.name" :value="item.code"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="类型" style="margin-left:20px;">
                  <el-select placeholder="选择商品类型" v-model="currentModify.goods_type">
                    <el-option v-for="item of goodsType" :label="item.key" :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="9">
                <el-form-item label="保障" style="margin-left:20px;">
                  <el-select placeholder="选择保障类型" v-model="currentModify.safe_type">
                    <el-option label="官方保障" value="0"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-form-item>
            <!--  -->
            <el-form-item label="图片">
              <el-upload
                list-type="picture-card"
                :limit="5"
                :action="$http.defaults.baseURL+'/upload/image'"
                :headers="headers"
                :on-preview="handleUploadPreview"
                :on-remove="handleUploadRemove"
                :on-error="handleUploadError"
                :on-success="handleUploadSuccess"
                :file-list="handleFileList()"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
            <el-form-item style="margin-bottom: 0;">
              <el-col :span="16">
                <div style="color:#F56C6C">1.图片最多只能添加五张，第一张会被设置成主图;</div>
                <div style="color:#F56C6C">2.过期时间设置为0，商品即是下架。</div>
              </el-col>
              <el-col :span="8">
                <el-button id="btn" type="primary" @click="handleUpdate" style="float:right">立即修改</el-button>
              </el-col>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-dialog>
    <el-dialog :visible.sync="imageVisible">
      <img width="100%" :src="imageUrl" alt />
    </el-dialog>
  </div>
</template>
<script>
import Table from "@/components/Table.vue";
export default {
  components: {
    Table
  },
  data() {
    return {
      imageUrl: "",
      imageVisible: false,
      dialogVisible: false,
      currentIndex: -1,
      headers: {
        Authorization: "Bearer " + localStorage.getItem("higame-token")
      },
      games: [],
      goodsType: [],
      requestData: {
        urlPath: "/trade/mylist",
        postData: {
          seller: "",
          keyword: "",
          start_price: "",
          end_price: "",
          belongs: "-1",
          goods_type: "-1",
          page_num: 1
        }
      },
      tableData: [],
      currentModify: {}
    };
  },
  methods: {
    listHander(e) {
      this.tableData = e;
      this.isLoaded = true;
    },
    search() {
      this.$refs.table.init();
    },
    getBelongs(belongs) {
      let game = this.games.find(item => {
        return item.code == belongs;
      });
      return game.name;
    },
    getGoodsType(type) {
      let goodsTypeItem = this.goodsType.find(item => {
        return item.value == type;
      });
      return goodsTypeItem.key;
    },
    handleShowDialog(index) {
      this.dialogVisible = true;
      this.currentModify = this.tableData[index];
    },
    handleUpdate(index) {
      let postData = JSON.parse(JSON.stringify(this.currentModify));
      if (postData.detail_image != null) {
        postData.detail_image = postData.detail_image.join(",");
      }

      this.$http
        .post("/trade/myupdate", postData)
        .then(res => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success"
            });
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(err => {
          this.$message.error(err.msg);
        });
    },
    async handleRemove(index) {
      await this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      });
      this.$http
        .post("/trade/delete", this.tableData[index])
        .then(res => {
          if (res.code == 200) {
            this.tableData.splice(index, 1);
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(err => {
          this.$message.error(err.msg);
        });
    },
    handleUploadPreview(file) {
      this.imageUrl = file.url;
      this.imageVisible = true;
    },
    handleUploadRemove(file, fileList) {
      if (fileList.length > 0) {
        this.currentModify.main_image = fileList[0].response
          ? fileList[0].response.data
          : fileList[0].name;
        let images = [];
        for (let i = 1; i < fileList.length; i++) {
          images.push(
            fileList[i].response ? fileList[i].response.data : fileList[i].name
          );
        }
        this.currentModify.detail_image = images;
      }
    },
    handleUploadError(err, file, fileList) {
      this.$message.error(err.toString());
    },
    handleUploadSuccess(res, file, fileList) {
      if (res.code == 200) {
        this.$message({
          message: "上传成功！",
          type: "success"
        });
        if (fileList.length > 0) {
          this.currentModify.main_image = fileList[0].response
            ? fileList[0].response.data
            : fileList[0].name;
          let images = [];
          for (let i = 1; i < fileList.length; i++) {
            images.push(
              fileList[i].response
                ? fileList[i].response.data
                : fileList[i].name
            );
          }
          this.currentModify.detail_image = images;
        }
      } else {
        this.$message.error(res.msg);
      }
    },
    handleFileList() {
      let files = [];
      if (this.currentModify.main_image != "") {
        files.push({
          name: this.currentModify.main_image,
          url:
            this.$http.defaults.baseURL +
            "/upload/image/" +
            this.currentModify.main_image
        });
      }
      if (this.currentModify.detail_image == null) {
        return files;
      }
      for (let item of this.currentModify.detail_image) {
        if (item != "") {
          files.push({
            name: item,
            url: this.$http.defaults.baseURL + "/upload/image/" + item
          });
        }
      }
      return files;
    },
    handleDialogClose() {
      this.currentModify = {};
    }
  },
  created() {
    this.$http
      .post("/game/list", this.postData)
      .then(res => {
        if (res.code == 200) {
          this.games = res.data.list;
        } else {
          this.$message.error(res.msg);
        }
      })
      .catch(err => {
        this.$message.error(err.msg);
      });
    this.$http
      .post("/trade/goodstype", {})
      .then(res => {
        if (res.code == 200) {
          this.goodsType = res.data;
        } else {
          this.$message.error(res.msg);
        }
      })
      .catch(err => {
        this.$message.error(err.msg);
      });
  }
};
</script>
<style>
.dialog {
  width: 750px;
}
.main-box {
  padding: 0 5px;
  background-color: #fff;
  overflow: hidden;
  color: #303133;
  transition: 0.3s;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style>



